package org.jheuristics.ga.operators;

import org.jheuristics.ga.GAConfig;
import org.jheuristics.ga.GAStatus;
import org.jheuristics.ga.Operator;
import org.jheuristics.ga.Population;

/**
 * TODO
 *
 * @author Marcell Manfrin, marcell@ourgrid.org, Jan 9, 2006
 */
public class PopulationInsertionOperator implements Operator {
    private PopulationChooser chooser;
    private Population individuals;

    /**
     * TODO
     *
     * @param chooser
     */
    public PopulationInsertionOperator(PopulationChooser chooser) {
        setPopulationChooser(chooser);
    }

    /**
     * TODO
     *
     * @param status
     * @param config
     * @see org.jheuristics.ga.Operator#operate(org.jheuristics.ga.GAStatus, org.jheuristics.ga.GAConfig)
     */
    public void operate(GAStatus status, GAConfig config) {
        if (individuals != null && individuals.size() > 0) {
            synchronized (this) {
                Population toPop = chooser.choose(status, config);
                toPop.addIndividuals(individuals);
                individuals = null;
            }
        }
    }

    /**
     * TODO
     *
     * @return
     */
    public PopulationChooser getPopulationChooser() {
        return chooser;
    }

    /**
     * TODO
     *
     * @param chooser
     */
    public void setPopulationChooser(PopulationChooser chooser) {
        if (null == chooser) {
            // TODO: ERROR MSG
            throw new NullPointerException();
        }
        this.chooser = chooser;
    }

    /**
     * TODO
     *
     * @param individual
     */
    public synchronized void setPopulation(Population population) {
        this.individuals = population;
    }

}
